package cn.knightzz.controller;


import cn.knightzz.entity.JsonResult;
import cn.knightzz.entity.SysRepository;
import cn.knightzz.service.SysRepositoryService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@RestController
public class SysRepositoryController {

    @Resource
    SysRepositoryService repositoryService;

    @PostMapping("/repository")
    public JsonResult<Object> addRepository(@RequestBody SysRepository repository) {
        Boolean status = repositoryService.addRepository(repository);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "添加成功!");
        } else {
            result = new JsonResult<>("201", "添加失败!");
        }
        return result;
    }

    @PutMapping("/repository")
    public JsonResult<Object> editRepository(@RequestBody SysRepository repository) {
        Boolean status = repositoryService.editRepository(repository);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "更新成功!");
        } else {
            result = new JsonResult<>("201", "更新失败!");
        }
        return result;
    }

    @DeleteMapping("/repository/{repoId}")
    public JsonResult<Object> deleteRepository(@PathVariable Long repoId) {
        Boolean status = repositoryService.deleteRepository(repoId);
        JsonResult<Object> result;
        if (status) {
            result = new JsonResult<>("200", "删除成功!");
        } else {
            result = new JsonResult<>("201", "删除失败!");
        }
        return result;
    }

    @GetMapping("/repository")
    public JsonResult<HashMap<String, Object>> selectRepoPage(@RequestParam("pageNum") Long pageNum,
                                                              @RequestParam("pageSize") Long pageSize,
                                                              @RequestParam("query") String query) {
        IPage<SysRepository> pages = repositoryService.selectRepoPage(pageNum, pageSize, query);
        long total = pages.getTotal();
        List<SysRepository> records = pages.getRecords();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("repository", records);

        JsonResult<HashMap<String, Object>> result = new JsonResult<>("200", "数据获取成功!");
        result.setData(data);
        return result;
    }

}

